package com.chuanyang.driverh.mainhome.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.MotorcadeMsgActivity;
import com.chuanyang.driverh.response2.join.ResultAddList;
import com.chuanyang.driverh.response2.join.ResultSearchList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.GlideCircleTransform;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class SearchDriverAdapter extends BaseListAdapter<ResultSearchList.DataBeanX.DataBean> {
    private Context mContext;

    public SearchDriverAdapter(Context context) {
        super(context);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.mContext = context;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.item_search_driver, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_jiameng = (ImageView) convertView.findViewById(R.id.iv_jiameng);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_jiameng_num = (TextView) convertView.findViewById(R.id.tv_jiameng_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ResultSearchList.DataBeanX.DataBean data = getItem(position);

        viewHolder.tv_name.setText(data.getFleetName());
        viewHolder.tv_jiameng_num.setText(data.getJoinedNum()+"人已加盟");
        viewHolder.iv_jiameng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击加盟
                addBusinessRelation(SharedPrefUtil.getUserId(),data.getFleetId()+"",
                        SharedPrefUtil.getUserId(),data.getFleetId()+"");
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView tv_name;
        TextView tv_jiameng_num;
        ImageView iv_jiameng;
    }

    //加盟接口
    private void addBusinessRelation(String initiator,String pendi,String driverId,String fleetId){

        DialogUtil.showLoadingDialog(mContext,"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("initiator", initiator);
        paramssss.put("pendi", pendi);
        paramssss.put("driverId", driverId);
        paramssss.put("fleetId", fleetId);

        MyHttpUtil.addBusinessRelation(mContext,paramssss, new MyCallback<Result>(mContext,
                new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                Toast.makeText(mContext,"加盟失败",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onSuccess(Result result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        Toast.makeText(mContext,result.getMsg(),Toast.LENGTH_SHORT).show();
                        LogUtils.d(result.getCode()+result.getMsg());
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(mContext,result.getMsg());
                }

            }
        });
    }

}
